package cn.antscrpt.antLib.test 
{

	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	import flash.geom.*;
	import flash.filters.*;
	
	import org.papervision3d.cameras.*;
	import org.papervision3d.materials.*;
	import org.papervision3d.objects.*;
	import org.papervision3d.objects.primitives.*
	import org.papervision3d.view.*;
	import org.papervision3d.view.layer.*;
	import org.papervision3d.core.render.filter.*;
	import org.papervision3d.core.effects.*;
	import org.papervision3d.core.effects.utils.*;
	
	import caurina.transitions.Tweener;
	
	[SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "#001020")]
	
	public class EX4 extends BasicView
	{	
		private var bfx:BitmapEffectLayer;
		
		/**
		 * Constructor
		 */
		public function EX4()
		{
			// init
			super(0, 0, true, false, CameraType.FREE);
			
			// create the effect layer
			bfx = new BitmapEffectLayer(viewport, 465, 465, true, 0, BitmapClearMode.CLEAR_PRE);
			bfx.addEffect(new BitmapLayerEffect( new BlurFilter(2, 2, 1)));
			bfx.addEffect(new BitmapColorEffect(1.2, 1.15, 1.1, .95));
			bfx.drawCommand = new BitmapDrawCommand(null, new ColorTransform(1, 1, 1, 0.5), BlendMode.ADD);
			bfx.drawLayer.blendMode = BlendMode.ADD;
			viewport.containerSprite.addLayer(bfx);
			
			// update
			var timer:Timer = new Timer(2000);
			timer.addEventListener(TimerEvent.TIMER, timerHandler);
			timer.start();
			
			// create the Field
			var field:Plane = new Plane(new WireframeMaterial(0xFFFFFF, .25), 5000, 5000, 5, 5);
			field.rotationX = 90;
			scene.addChild(field);
			
			// init camera
			camera.y = 200;
			camera.z = -1000;
			camera.rotationX = -20;
			camera.focus = 400;
			camera.zoom = 1;
                        
                        // rotate angle
			stage.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void
			{
				camera.y = camera.y == 200 ? 1500 : 200;
				camera.rotationX = camera.rotationX == -20 ? 45 : -20;
			});
			
			// render
			startRendering();
		}
		
		/**
		 * Timer Handler - Give Fireworks
		 * @param	event
		 */
		private function timerHandler(event:TimerEvent):void
		{
			var fireworks:DisplayObject3D = createFireworks();
			fireworks.x = Math.random() * 500 - 250;
			fireworks.z = Math.random() * 500 - 250;
			fireworks.y = Math.random() * 250 + 750;
			
			scene.addChild(fireworks);
			
			Tweener.addTween(fireworks,
			{
				y          : fireworks.y  - 200,
				time       : 4,
				transition : "easeInSine"
			});
		}
		
		/**
		 * Create Fireworks
		 * @return Fireworks(DisplayObject3D)
		 */
		private function createFireworks():DisplayObject3D
		{
			var fireworks :DisplayObject3D = new DisplayObject3D();
			
			var points:Array = getSpherePoints(300, 10);
			
			var color:uint = Math.random() * 0xFFFFFF;
			
			for (var i:int = 0; i < points.length; i++ )
			{
				var mat:ColorMaterial = new ColorMaterial(color);
				mat.doubleSided = true;
				
				var particle:Plane = new Plane(mat, 5, 5, 1, 1);
				
				fireworks.addChild(particle);
				
				bfx.addDisplayObject3D(particle);
				
				Tweener.addTween(particle,
				{
					x     : points[i].x,
					y     : points[i].y,
					z     : points[i].z,
					time  : 3 + Math.random() * .5,
					transition : "easeOutCubic",
					onCompleteParams : [particle],
					onComplete : function(p:Plane):void
					{
						fireworks.removeChild(p);
						bfx.removeDisplayObject3D(p);
					}
				});
			}
			
			return fireworks;
		}
		
		/**
		 * Get Points of sphere
		 * @param	size
		 * @param	segment
		 * @return  point of shphere
		 */
		private function getSpherePoints(size:int, segment:int):Array
		{
			var points:Array = [];
			
			var sphere:Sphere = new Sphere(new WireframeMaterial(), size, segment, segment);
			
			for each(var i:* in sphere.geometry.vertices)
				points.push( { x:i.x, y:i.y, z:i.z } );
			
			return points;
		}

	}
}